package output.input.cwiczenia;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.util.Date;

public class OdczytZapisObiektow {

    public static void main(String[] args) {

        try {
            File f = new File("serialowyBackup.ser");
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
            oos.writeObject("Alicja ma kocurka.");
            oos.writeObject(new Date());
            oos.close();
            System.out.println("Zapisano plik " + f.getAbsolutePath() + "\n");
        } catch (FileNotFoundException e) {
            System.out.println( );
        } catch (IOException e) {
            //
        }

        File f = new File("serialowyBackup.ser");
        System.out.println("Odczytam plik " + f.getAbsolutePath() + "\n");
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
            System.out.println((String) ois.readObject());
            System.out.println(DateFormat.getDateInstance().format((Date) ois.readObject()));
            ois.close();
        } catch (FileNotFoundException e) {
            System.out.println("Nie odnaleziono pliku");
        } catch (IOException e) {
            //
        } catch (ClassNotFoundException e) {
            //
        }
    }

}
